package com.tmc.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.entity.common.EthAgent;
import com.tmc.entity.common.EthClient;
import com.tmc.util.ObjectUtil;

@Repository
public class EthClientDaoImpl extends BaseDaoImpl<EthClient>{
	
	public Pagination<EthClient> findByCon(ContextInfo context, EthClient cond, int pageIndex, int pageSize) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthClient c where c.enabledFlag = 'Y' ");
		
		if (cond.getClientId() != null) {
			sql.append(" and (c.clientId = :clientId) ");
			params.put("clientId", cond.getClientId());
		}
		if (ObjectUtil.hasText(cond.getClientName())) {
			sql.append(" and (c.clientName like :clientName) ");
			params.put("clientName", cond.getClientName() + "%");
		}
		if (ObjectUtil.hasText(cond.getClientType())) {
			sql.append(" and (c.clientType = :clientType) ");
			params.put("clientType", cond.getClientType());
		}
		sql.append(" order by c.lastUpdateDate desc ");
		return this.findPagination(sql.toString(), params, pageIndex, pageSize);
	}
	
	
	public List<EthClient> findAll(ContextInfo context) {
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthClient c where c.enabledFlag = 'Y' ");
		return this.findList(sql.toString(), params);
	}
	
	public EthAgent getByCode(ContextInfo context, String clientCode) {
		if (!ObjectUtil.hasText(clientCode)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthClient c where c.enabledFlag = 'Y' ");
		sql.append(" and c.clientCode = :clientCode ");
		params.put("clientCode", clientCode);
		return this.get(sql.toString(), params);
	}
	
	public EthClient getByClientName(ContextInfo context, String clientName) {
		if (!ObjectUtil.hasText(clientName)) {
			return null;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthClient c where c.enabledFlag = 'Y' ");
		sql.append(" and c.clientName = :clientName ");
		params.put("clientName", clientName);
		return this.get(sql.toString(), params);
	}
	
	
	public List<EthClient> findByClientName(String clientName){
		Map<String, Object> params = new HashMap<String, Object>();
		StringBuilder sql = new StringBuilder();
		sql.append(" from EthClient c where c.enabledFlag = 'Y' ");
	
		if (ObjectUtil.hasText(clientName)) {
			sql.append(" and (c.clientName like :clientName) ");
			params.put("clientName", clientName + "%");
		}
		return this.findList(sql.toString(), params);		
	}
}
